 /*
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.Container;
import java.awt.event.*;
import javax.swing.*;
import org.jdesktop.layout.GroupLayout;

public class OEWsi extends JFrame {

    public OEWsi(MTWsi mtws, int nrSp, int nrW) {
        this.nrW = nrW;
        this.nrSp = nrSp;
        mt = mtws;
        nowa = true;
        initComponents();
        jLabel3.setText((new StringBuilder()).append(nrSp).append(".").toString());
        jTextField2.setText((new StringBuilder()).append(nrW).append("").toString());
    }

    public OEWsi(MTWsi mtws, String naz, int nrSp, int nrW, int nr) {
        mt = mtws;
        this.naz = naz;
        nowa = false;
        this.nr = nr;
        this.nrSp = nrSp;
        this.nrW = nrW;
        initComponents();
        jTextField1.setText(naz);
        jLabel3.setText((new StringBuilder()).append(nrSp).append(".").toString());
        jTextField2.setText((new StringBuilder()).append(nrW).append("").toString());
        setTitle("Edycja miejscowo\u0139\u203Aci");
    }

    private void initComponents() {
        jPanel1 = new JPanel();
        jTextField1 = new JTextField();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        jTextField2 = new JTextField();
        jButton1 = new JButton();
        jButton2 = new JButton();
        setDefaultCloseOperation(2);
        setTitle("Dodawanie nowej miejscowo\u015Bci");
        jPanel1.setBorder(BorderFactory.createEtchedBorder());
        jTextField1.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
            final OEWsi this$0;
            

            {
                this$0 = OEWsi.this;
            //super();
            }
        });
        jLabel1.setText("Nazwa");
        jLabel2.setText("ID");
        jLabel3.setText("id");
        jTextField2.setText("id");
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(1).add(jLabel1).add(jLabel2)).addPreferredGap(0, -1, 32767).add(jPanel1Layout.createParallelGroup(1).add(jTextField1, -2, 243, -2).add(jPanel1Layout.createSequentialGroup().add(jLabel3).add(3, 3, 3).add(jTextField2, -2, 18, -2))).addContainerGap(23, 32767)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(1).add(2, jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(3).add(jLabel2).add(jLabel3).add(jTextField2, -2, -1, -2)).addPreferredGap(0, -1, 32767).add(jPanel1Layout.createParallelGroup(3).add(jTextField1, -2, -1, -2).add(jLabel1)).addContainerGap()));
        jButton1.setText("Ok");
        jButton1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OEWsi this$0;
            

            {
                this$0 = OEWsi.this;
            //super();
            }
        });
        jButton1.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent evt) {
                jButton1KeyPressed(evt);
            }
            final OEWsi this$0;
            

            {
                this$0 = OEWsi.this;
            //super();
            }
        });
        jButton2.setText("anuluj");
        jButton2.setDefaultCapable(false);
        jButton2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OEWsi this$0;
            

            {
                this$0 = OEWsi.this;
            //super();
            }
        });
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(jPanel1, -2, -1, -2).addPreferredGap(0).add(layout.createParallelGroup(1).add(jButton1, -1, 73, 32767).add(jButton2)).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(2, layout.createSequentialGroup().addContainerGap(-1, 32767).add(layout.createParallelGroup(1).add(jPanel1, -2, -1, -2).add(2, layout.createSequentialGroup().add(jButton1).addPreferredGap(0).add(jButton2).add(12, 12, 12)))));
        pack();
    }

    private void jTextField1KeyPressed(KeyEvent evt) {
        int key = evt.getKeyCode();
        KeyEvent _tmp = evt;
        if (key == 10) {
            zakoncz();
        }
    }

    private void jButton1KeyPressed(KeyEvent evt) {
        int key = evt.getKeyCode();
        KeyEvent _tmp = evt;
        if (key == 10) {
            zakoncz();
        }
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        dispose();
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        zakoncz();
    }

    private void zakoncz() {
        try {
            int d = Integer.parseInt(jTextField2.getText());
            if (nowa) {
                mt.addRow(d, jTextField1.getText());
            } else {
                mt.editRow(d, nr, jTextField1.getText());
            }
            dispose();
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "B\u0139\u201Aad podczas wprowadzania danych - (sprawdz czy pola liczbowe nie zawieraj\304\u2026 liter)");
        }
    }
    private MTWsi mt;
    private int nr;
    private int nrW;
    private int nrSp;
    private String naz;
    private boolean nowa;
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JPanel jPanel1;
    private JTextField jTextField1;
    private JTextField jTextField2;
}
